<?php
	$url = $_REQUEST['url'];
	if ($url != "") {
		
		if (isset($_REQUEST['f_tfg']) && $_REQUEST['f_tfg'] == true) {
			$tfg = json_decode(file_get_contents("http://ecat-dev.gbif.org/tf?type=url&format=json&input=$url"), true);
		}
		if (isset($_REQUEST['f_lucene']) && $_REQUEST['f_lucene'] == true) {
			$lucene = json_decode(file_get_contents("http://ecat-dev.gbif.org/ws/indexer?type=url&input=$url&format=json"), true);
		}		
		if (isset($_REQUEST['f_tfu']) && $_REQUEST['f_tfu'] == true) {
			$tfu = json_decode(file_get_contents("http://namefinding.ubio.org/find?type=url&client=taxonfinder&format=json&input=$url"), true);
		}		
		if (isset($_REQUEST['f_neti']) && $_REQUEST['f_neti'] == true) {
			$neti = json_decode(file_get_contents("http://namefinding.ubio.org/find?type=url&client=neti&format=json&input=$url"), true);	
		}
	
		$html = file_get_contents($url);
		$enc = mb_detect_encoding($html);
		include_once("../../resources/api/class.badsectors.php");
		$bs = new BadSectors($html, $enc);

	}

	function processList($list, $enc, $os = 0) {
#print_r($list);		
		global $bs;
		global $html;
		$j = $list;
		for($i= count($j)-1; $i>=0; $i--) {
			$rec = $j[$i];
#			print_r($rec);
			print "Verbatim: " . $rec["verbatim"] . "<br>";
			print "Sci. Name: " . $rec["scientificName"] . "<br>";
			print "Offset: " . $rec["offsetStart"] . "-" . $rec["offsetEnd"] . "<br>";
			$color1 = "black";
			$color2 = "black";

			if ($rec["offsetStart"] != "" && $rec["offsetEnd"] != "") {				
				$val = mb_substr($html, $rec["offsetStart"] + $os, $rec["offsetEnd"]-$rec["offsetStart"], $enc);
				if ($val === $rec["verbatim"]) $color1 = "green";
				print "<span style='color:$color1'>MultiByte::" . $val . "::</span><br>";
				$val = substr($html, $rec["offsetStart"] + $os, $rec["offsetEnd"]-$rec["offsetStart"]);
				if ($val === $rec["verbatim"]) $color2 = "green";
				print "<span style='color:$color2'>SubString::" . $val . "::</span><br>";
				$s1 = $bs->isSafe($rec["offsetStart"] + $os);
				if (!$s1) {
					print_r($bs->getLastBadSector());
					print "<br>";
				}
				$s2 = $bs->isSafe($rec["offsetEnd"] + $os);
				if (!$s2) {
					print_r($bs->getLastBadSector());
					print "<br>";
				}
			} else {
				print "Empty offsets!";
			}
			print "<hr size=\"1\" noshade>";
		}

	}
	
?>
<html>
<style type="text/css">
<!--
body,td,th {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}
hr {
	height: 1px;
}
-->
</style>
<body>
<?php

	if ($url != "") {		
		print "Detected Encoding: " . $enc . "<br>"; ?>
<table width="100%" border="0" cellpadding="2" cellspacing="0">
  <tr>
    <td bgcolor="#CCC"><strong>TaxonFinder ubio</strong></td>
    <td bgcolor="#CCC"><strong>TaxonFinder GBIF</strong></td>
    <td bgcolor="#CCC"><strong>NetiNeti</strong></td>
    <td bgcolor="#CCC"><strong>Lucene</strong></td>
  </tr>
  <tr>
    <td valign="top"><? print "<div><b>Names Found: " . count($tfu["names"]) . "</b></div><div>" . processList($tfu['names'], $enc); ?></div></td>
    <td valign="top" bgcolor="#EFEFEF"><? print "<div><b>Names Found: " . count($tfg["names"]) . "</b></div>" . processList($tfg['names'], $enc); ?></td>
    <td valign="top"><? print "<div><b>Names Found: " . count($neti["names"]) . "</b></div>" . processList($neti['names'], $enc, 1); ?></td>
    <td valign="top" bgcolor="#EFEFEF"><? print "<div><b>Names Found: " . count($lucene["names"]) . "</b></div>" . processList($lucene['names'], $enc); ?></td>
  </tr>
</table>

<?php
		if (isset($_REQUEST["show_sectors"]) && $_REQUEST["show_sectors"]) {
			print "<pre>";
				print_r($bs->sectors);
			print "</pre>";
		}
	}
?>
<br>
<form name="form1" method="get" action="">
  <p>
    <label>Url:
      <input name="url" type="text" id="url" value="<?php print $url; ?>" size="40">
    </label>
  </p>
  <p>
    <label>
      <input name="f_neti" type="checkbox" id="f_neti" value="1" checked>
      NetiNeti</label>
    <br>
    <label>
      <input name="f_tfu" type="checkbox" id="f_tfu" value="1" checked>
      uBio TF</label>
    <br>
    <label>
      <input name="f_tfg" type="checkbox" id="f_tfg" value="1" checked>
      GBIF TF</label>
    <br>
    <label>
      <input name="f_lucene" type="checkbox" id="f_lucene" value="1" checked>
      GBIF Lucene</label>
    <br>
    <label>
      <input name="show_sectors" type="checkbox" id="show_sectors" value="1" checked>
      Show Bad Sectors</label>
  </p>
  <p>
    <label>
      <input type="submit" name="button" id="button" value="Submit">
    </label>
    <br>
  </p>
</form>
<p>&nbsp;</p>
</body>
</html>